﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
namespace DAO
{
    public class DangKySuatChieuDAO
    {
        //KIỂM TRA THÔNG TIN TRƯỚC KHI ĐĂNG KÝ
        public static int KiemTra_DangKy(DangKySuatChieu DKSC)
        {
            SqlConnection cn;
            cn = DataProvider.ConnectionData();

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SP_KIEMTRA_DANGKY";
            cmd.Connection = cn;

            SqlParameter para;
            para = new SqlParameter("MAPHIM", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = DKSC.MaPhim;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MAPHONG", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = DKSC.MaPhongChieu;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MARAP", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = DKSC.MaRap;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MASC", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = DKSC.MaSuatChieu;
            cmd.Parameters.Add(para);

            para = new SqlParameter("NGAYCHIEU", SqlDbType.Date);
            para.Direction = ParameterDirection.Input;
            para.Value = DKSC.NgayChieu;
            cmd.Parameters.Add(para);

            para = new SqlParameter("KQ", SqlDbType.Int);
            para.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(para);


            cmd.ExecuteNonQuery();
            int KQ = (int)cmd.Parameters["KQ"].Value;
            cn.Close();
            return KQ;

        }
        //đăng ký suất chiếu
        public static int DangKySuatChieu(DangKySuatChieu DKSC)
        {
            SqlConnection cn;
            cn = DataProvider.ConnectionData();

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SP_DANGKYSUATCHIEU";
            cmd.Connection = cn;

            SqlParameter para;
            para = new SqlParameter("MAPHIM", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = DKSC.MaPhim;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MAPHONG", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = DKSC.MaPhongChieu;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MARAP", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = DKSC.MaRap;
            cmd.Parameters.Add(para);

            para = new SqlParameter("MASC", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = DKSC.MaSuatChieu;
            cmd.Parameters.Add(para);

            //định dạng lại ngày chiếu trước khi đưa xuống database
            string typedate = DKSC.NgayChieu.ToString("yyyy-MM-dd");
            para = new SqlParameter("NGAYCHIEU", SqlDbType.Date);
            para.Direction = ParameterDirection.Input;
            para.Value = typedate;
            cmd.Parameters.Add(para);

            para = new SqlParameter("KQ", SqlDbType.Int);
            para.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(para);


            cmd.ExecuteNonQuery();
            int KQ = (int)cmd.Parameters["KQ"].Value;
            cn.Close();
            return KQ;

        }
    }
}
